import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage.js'
Vue.use(Vuex)
const TOKEN_KEY = 'TOUTIAO_USER'
export default new Vuex.Store({
  state: {
    // user: '' // 存储用户登录信息
    // 一个对象,存储当前用户登录信息
    // user: JSON.parse(window.localStorage.getItem(TOKEN_KEY))
    user: getItem(TOKEN_KEY)
  },
  getters: {
  },
  mutations: {
    getUser (state, user) {
      state.user = user
      // 为了防止数据丢失,将数据备份到本地存储(本地存储只能存字符串)
      // window.localStorage.setItem(TOKEN_KEY, JSON.stringify(user))
      setItem(TOKEN_KEY, user)
    }
  },
  actions: {
  },
  modules: {
  }
})
